home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FDF101.ARJ / ELIB.ZOO / hashpjw.c < prev    next >
C/C++ Source or Header  |  1992-04-30  |  628b  |  36 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dir.h>
  4.  
  5. #include "elib.h"
  6.  
  7.  
  8.  
  9. /*
  10.  * hashpjw
  11.  *
  12.  * hash function.  Used in P. J. Weinberger's portable C compiler and
  13.  * gotten from page 436 of the Dragon book on compilers (Compilers: Principles,
  14.  * Techniques and Tools, by Aho, Sethi and Ullman.  Copyright (c) 1986 by
  15.  * Bell Telephone Laboratories).
  16.  */
  17. int hashpjw(char *s)
  18.  
  19. {
  20.     char *p;
  21.     unsigned long h = 0, g;
  22.  
  23.     g = h & 0xf0000000;
  24.  
  25.     for (p = s; *p != '\0'; p++) {
  26.         h = (h << 4) + (*p);
  27.         g = h & 0xf0000000;
  28.         if (g) {
  29.             h ^= (g >> 24);
  30.             h ^= g;
  31.         }
  32.     }
  33.  
  34.     return h % HASH_TAB_SIZE;
  35. }
  36.